package com.example.testapplication.activity

import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.example.testapplication.MyView
import com.example.testapplication.R
import com.example.testapplication.databinding.ActivityTestBinding
import com.example.testapplication.rolling.Rolling
import com.example.testapplication.rolling.RollingStatus
import com.example.testapplication.viewModel.MainViewModel

class TestActivity : AppCompatActivity() {

    private lateinit var binding: ActivityTestBinding
    private val mainViewModel: MainViewModel by viewModels()
    private lateinit var myView: MyView

//    private lateinit var workManager: WorkManager


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        myView = MyView(this)
//        workManager = WorkManager.getInstance(this)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_test)
        binding.viewModel = mainViewModel
        binding.lifecycleOwner = this

        binding.moveLayout.gravity = Rolling.right
        var i = false
        var f = 0.0f
        binding.text.setOnClickListener {
            i = !i
            f += 1f
            val status = RollingStatus()

            val view = TextView(this)
            if (i) {
                status.speed = 10

                view.setBackgroundColor(ContextCompat.getColor(this, R.color.teal_200))
                view.text = "我就是要跳$f"
            } else {
                status.speed = 0

                view.setBackgroundColor(ContextCompat.getColor(this, R.color.purple_200))
                view.text = "我就是要跳$f"
            }


            binding.moveLayout.addRollingView(view, status)
        }

        binding.moveLayout.setOnChildClickListener { view, rollingStatus ->
            if (view is TextView) {
                Log.e("moveLayout", " --->${view.text}")
            }
        }

        binding.btMove.setOnClickListener {
            binding.moveLayout.gravity =
                if (binding.moveLayout.gravity == Rolling.right) Rolling.left else Rolling.right
            myView.logActivity(this)


//            //        OneTimeWorkRequest:WorkRequest只会执行一次的A。
//            //        PeriodicWorkRequest:WorkRequest一个周期将重复的A
//            val data = Data.Builder()
//            data.putString("data20", "1我就是数据1")
////            val obj = OneTimeWorkRequest.Builder(BlurWorker::class.java).setInputData(data.build()).build()
//            val per = PeriodicWorkRequest.Builder(BlurWorker::class.java, 15L, TimeUnit.MINUTES)
//                .setInputData(data.build()).build()
//            workManager.enqueue(per)


        }

//        val time = Timer()
//        val timerTask = object : TimerTask() {
//            override fun run() {
//                Handler(Looper.getMainLooper()).post {
//                    binding.moveLayout.move()
//                }
//            }
//        }
//        time.schedule(timerTask, 0, 40)
    }

}